package com.itqf.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.itqf.dto.TableData;
import com.itqf.dto.TableParam;
import com.itqf.entity.SysMenu;
import com.itqf.mapper.SysMenuMapper;
import com.itqf.service.SysMenuService;
import com.itqf.utils.SysConstant;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/25
 * @Time: 下午5:25
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private JedisPool jedisPool;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public R leftMenuList(long uid) {

        Jedis jedis = jedisPool.getResource();
        String json = jedis.get(SysConstant.LEFT_MENU_KEY);
        if (json!=null&&!json.equals("")){
            System.out.println("走redis缓存！！");
            R r = JSON.parseObject(json,R.class);
            return  r;
        }

        System.out.println("走数据库！！");
        //1.查询目录
        List<SysMenu> dirMenu = sysMenuMapper.findDirMenuByUserId(uid);
        //2.查询该目录下的子菜单
        for (SysMenu sysMenu : dirMenu) {
            //3.根据目录的编号查询子菜单
            // 得到菜单的parent_id
            long parentId = sysMenu.getMenuId();
            List<SysMenu> menuList =  sysMenuMapper.findMenuByUserId(uid,parentId);
            //4.把子菜单添加给目录
            sysMenu.setList(menuList);
        }

            //5.查询权限   "permissions": ["sys:schedule:info", "sys:menu:update", "sys:menu:delete", "sys:config:info", "sys:generator:list", "sys:menu:list", "sys:config:save", "sys:menu:perms", "sys:config:update", "sys:schedule:resume", "sys:user:delete", "sys:config:list", "sys:user:update", "sys:role:list", "sys:menu:info", "sys:menu:select", "sys:schedule:update", "sys:schedule:save", "sys:role:select", "sys:user:list", "sys:menu:save", "sys:role:save", "sys:schedule:log", "sys:role:info", "sys:schedule:delete", "sys:role:update", "sys:schedule:list", "sys:user:info", "sys:generator:code", "sys:schedule:run", "sys:config:delete", "sys:role:delete", "sys:user:save", "sys:schedule:pause", "sys:log:list"]
            //作业
        List<String> perms = sysMenuMapper.findPermsuByUserId(uid);

        Set<String> permss = new HashSet<>();//set:不重复和存取无序
        for (String perm : perms) {
            //商品  images  aaa.jpg,vvv.jpg,bbb.jpg
            // null   ""   "sys:menu:save,sys:menu:find"
            if (perm!=null&&!perm.equals("")){
                String s [] = perm.split(",");
                for (String s1 : s) {
                    permss.add(s1);
                }
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("menuList",dirMenu);
        map.put("permissions",permss);
         R r =  R.ok(map);
         json = JSON.toJSONString(r);

         jedis.set(SysConstant.LEFT_MENU_KEY,json);
        jedis.close();
        return  r;//{ code:0 data:{}}
    }

    @Override
    public TableData findMenuByPage(TableParam param) {

        List<SysMenu> list = sysMenuMapper.findMenuByPage(param);
        int total = sysMenuMapper.findTotalMenu(param);
        TableData data = new TableData();
        data.setRows(list);
        data.setTotal(total);//总的记录数

        return data;
    }

    @Override
    public R menuTree() {

        List<SysMenu> list = sysMenuMapper.findMenuTree();

        return R.ok(list);
    }

    @Override
    public R save(SysMenu sysMenu) {

        System.out.println("--->add前"+sysMenu.getMenuId());
        int i = sysMenuMapper.save(sysMenu);
        System.out.println("--->add后"+sysMenu.getMenuId());
        //管理员左侧可以展示新增的菜单
        //返回自增主键  role_id(1),menu_id
        sysMenuMapper.saveRoleMenu(SysConstant.ADMIN_ID,sysMenu.getMenuId());
        R r = null;
        if(i>0){
           r = R.ok(null);
           Jedis jedis =  jedisPool.getResource();
           jedis.del(SysConstant.LEFT_MENU_KEY);
           jedis.close();
        }else{
            r = R.failed("新增失败");
        }

        return r;
    }

    @Override
    public R findMenuById(long menuId) {
        SysMenu sysMenu = sysMenuMapper.findMenuById(menuId);
        return R.ok(sysMenu);
    }

    @Override
    public R updateMenu(SysMenu sysMenu) {
        int i = sysMenuMapper.update(sysMenu);
        return i>0?R.ok(null):R.failed("修改失败") ;
    }

    @Override
    public R deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            if(id<31){
                return R.failed("系统默认菜单，不能删除！");
            }
        }
        int i = sysMenuMapper.deleteBatch(ids);

        return i>0?R.ok(null):R.failed("删除失败");
    }
}
